<template>
    <div>
        <el-tabs
            v-if="options.length"
            v-model="activeIndex"
            class="tab-nav"
            type="card"
            closable
            @tab-click="tabClick"
            @tab-remove="tabRemove"
        >
            <el-tab-pane
                v-for="option in options"
                :key="option.route"
                :label="language=='en'?option.labelEn:option.label"
                :name="option.route"
            />
        </el-tabs>
        <div id="rigthContextMenu" class="right-menu">
            <ul>
                <li @click="closeAll">{{ $t("message.closeAll") }}</li>
                <li @click="closeOther">{{ $t("message.closeOther") }}</li>
            </ul>
        </div>
    </div>
</template>
<script>
export default {
    name: 'CustomTabs',
    data() {
        return {
            currentPane: ''
        }
    },
    computed: {
        language() {
            return this.$store.getters.language
        },
        options() {
            return this.$store.state.common.tabs.filter(item => item.name)
        },
        activeIndex: {
            get() {
                return this.$store.state.common.activeIndex
            },
            set(val) {
                this.$store.commit('set_active_index', val)
            }
        },
        params: {
            get() {
                const curTabs = this.$store.state.common.tabs.filter(item => {
                    return item.route == this.$store.state.common.activeIndex
                })
                // 处理清楚首页跳转时带的参数 query置为空对象
                if (
                    curTabs.length &&
                    curTabs[0].query &&
                    curTabs[0].query.isHomeJump
                ) {
                    curTabs[0].query = {}
                }
                return curTabs[0] && curTabs[0].query
                    ? Object.assign({}, curTabs[0].query)
                    : {}
            }
        }
    },
    watch: {
        $route(to) {
            const flag = this.options.some((item, index, array) => {
                return item.name === to.name
            })
            const path = to.path

            const label = this.$store.getters.getLabelByPath(path)
            const tab = {
                route: path,
                name: to.name,
                label: label.authNameCh,
                labelEn: label.authNameEn,
                query: to.query
            }
            if (!flag) {
                this.$store.commit('add_tabs', tab)
            }
            const index = this.options.findIndex(
                element => to.name === element.name
            )
            if (index !== -1) this.options[index] = tab
            this.setTabsLocal()
            this.$store.commit('set_active_index', path)
        }
    },
    mounted() {
        this.setTabsStore()
        const that = this
        const rm = document.getElementById('rigthContextMenu')
        document.documentElement.oncontextmenu = function(e) {
            e = e || window.event
            let paneName = ''
            if (
                e.target.className.indexOf(
                    'el-tabs__item is-top is-active is-closable'
                ) == 0 ||
                e.target.className.indexOf(
                    'el-tabs__item is-top is-closable'
                ) == 0
            ) {
                paneName = e.target.id
            } else if (e.target.className.indexOf('el-icon-close') == 0) {
                paneName = e.target.parentNode.id
            }
            if (paneName && paneName.length > 4) {
                paneName = paneName.substring(4)
                that.currentPane = paneName
                rm.style.display = 'block'
                rm.style.left = e.clientX + 'px'
                rm.style.top = e.clientY + 'px'
                return false
            }
        }
        document.onmousedown = function(event) {
            const target = event.target.parentNode
            if (
                target.nodeName == 'UL' &&
                target.parentNode &&
                target.parentNode.className === 'right-menu'
            ) {
                return
            }
            rm.style.display = 'none'
        }
    },
    methods: {
        tabClick(tab) {
            this.$router.replace({
                path: this.activeIndex,
                query: this.params
            })
        },
        tabRemove(targetName) {
            this.$store.commit('delete_tabs', targetName)
            this.setTabsLocal()
            if (this.activeIndex === targetName) {
                // 设置当前激活的路由
                if (this.options && this.options.length >= 1) {
                    this.$store.commit(
                        'set_active_index',
                        this.options[this.options.length - 1].route
                    )
                    this.$router.push({ path: this.activeIndex, query: this.params })
                } else {
                    this.$router.push({ path: '/workbench' })
                }
            }
        },
        setTabsLocal(tab) {
            const tabs = this.$store.state.common.tabs.slice(0)
            localStorage.setItem('tabs', JSON.stringify(tabs))
        },
        setTabsStore() {
            let tabs
            if (localStorage.hasOwnProperty('tabs')) {
                tabs = JSON.parse(localStorage.getItem('tabs'))
                tabs.forEach(ele => {
                    if (ele.name !== 'workbench') { this.$store.commit('add_tabs', ele) }
                })
            }
            // // 刷新界面路由仍是在当前路由上
            let current
            // 当前存在
            if (localStorage.getItem('setActiveIndex')) {
                current = localStorage.getItem('setActiveIndex')
                if (current != this.$route.path) {
                    if (tabs.findIndex(e => e.route == this.$route.path) > -1) {
                        this.$router.replace({
                            path: current,
                            query: this.params
                        })
                    }
                }
            } else {
                current = this.$route.path
                const to = this.$route

                const label = this.$store.getters.getLabelByPath(to.path)
                const tab = {
                    route: to.path,
                    name: to.name,
                    label: label.authNameCh,
                    labelEn: label.authNameEn,
                    query: to.query
                }

                this.$store.commit('add_tabs', tab)
            }
            this.$store.commit('set_active_index', current)
        },
        closeAll() {
            this.$store.commit('delete_tabs_exclude', ['/workbench'])
            this.setTabsLocal()
            this.$store.commit('set_active_index', '/workbench')
            this.$router.push({ path: '/workbench' })
            document.getElementById('rigthContextMenu').style.display = 'none'
        },
        closeOther() {
            this.$store.commit('delete_tabs_exclude', [
                '/workbench',
                this.currentPane
            ])
            this.setTabsLocal()
            if (this.currentPane !== this.activeIndex) {
                this.$store.commit('set_active_index', this.currentPane)
                this.$router.push({ path: this.currentPane })
            }
            document.getElementById('rigthContextMenu').style.display = 'none'
        }
    }
}
</script>
<style lang="scss" scoped>
.right-menu {
    display: none;
    position: absolute;
    border: 1px solid #ccc;
    background: #fff;
    z-index: 10;
    & > ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }
    & li {
        width: 100px;
        text-align: center;
        line-height: 32px;
    }
    & li:hover {
        background: #ebf2ff;
        color: #3a7fe8;
        cursor: pointer;
    }
}
</style>
